﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;

namespace CrawlerCore
{
    public static partial class GeneralClass
    {
        public static string GetFullUrl(string parentUrl, string childUrl)
        {
            string fullUrl = "";
            if (string.IsNullOrEmpty(childUrl))
            {
                fullUrl = parentUrl + "/";
            }
            else
            {
                if (!childUrl.Contains(parentUrl + "/"))
                {
                    List<string> list = new List<string>();
                    string[] paths = parentUrl.Replace("http://", "").Split(new char[] { '/' });
                    list.AddRange(paths);
                    paths = childUrl.Split(new char[] { '/' });
                    foreach (string path in paths)
                    {
                        if (!list.Contains(path)
                            && !string.IsNullOrEmpty(path))
                            list.Add(path);
                    }
                    fullUrl = "http://" + string.Join("/", list.ToArray());
                }
                else
                    fullUrl = childUrl;
            }
            return fullUrl;
        }

        ///<summary>
        /// Get name of a website from url string
        ///</summary>
        ///<param name="url"></param>
        ///<returns></returns>
        public static string GetName(string url)
        {
            string pattern1 = @"(http(s|)://|)(www\.|)";
            //"(?<=\")(?<value>.*)(?=\")\\;"
            Regex r1 = new Regex(pattern1);
            //string pattern2 = "(.com|.org|.co.uk|.net|.info)(/|).*";
            string pattern2 = "/.*";
            Regex r2 = new Regex(pattern2);
            Match m;

            string forumname = url.ToLower();
            m = r1.Match(forumname);

            //stem head
            if (m.Success)
                forumname = forumname.Replace(m.Value, "");

            //Stem tail
            m = r2.Match(forumname);
            if (m.Success)
            {
                forumname = forumname.Replace(m.Value, "");
            }
            return forumname;
        }

        public static string FormatUrl(string url)
        {
            string result = url;
            string pattern = @"/\w+(\.php|\.html).*";
            //string pattern = @"(/\w+(\.php|\.html).*$|/$)";
            Regex r = new Regex(pattern);
            Match m;

            //Stem tail
            m = r.Match(url);
            //if (m.Success)
            result = m.Success ? url.Replace(m.Value, "") : result;

            pattern = @"/$";
            r = new Regex(pattern);
            m = r.Match(result);
            result = m.Success ? result.Substring(0, result.Length - 1) : result;
            return result;
        }

        public static string FormatUrl2(string url)
        {
            string result = url.Replace("http://","");
            string[] path = result.Split(new char[] { '/' });
            result = "http://" + path[0];
            if (path.Length > 1){
            string pattern = @"(forum(s|)|diendan|foro(s|))";
            //string pattern = @"(/\w+(\.php|\.html).*$|/$)";
            Regex r = new Regex(pattern);
            Match m;
            m = r.Match(path[1]);
            if (m.Success)
                result += "/" + path[1];
            }
            return result;
        }

        
    }
}